//
//  BoneViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/28.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "BoneViewController.h"
#import "PGDatePickManager.h"
#import "BaziLifeInfoViewController.h"

@interface BoneViewController ()<PGDatePickerDelegate>

@property (nonatomic, weak)UILabel * topLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, strong)UIView * oneView;
@property (nonatomic, strong)UIView * twoView;
@property (nonatomic, strong)UIView * threeView;
@property (nonatomic, strong)UIView * fourView;
@property (nonatomic, weak)UITextField * nameTextFiled;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * genderLabel;
@property (nonatomic, strong)UILabel * briDateLabel;
@property (nonatomic, strong)UILabel * briTimeLabel;
@property (nonatomic, strong)UIButton * genderButton;
@property (nonatomic, strong)UIButton * briDateButton;
@property (nonatomic, strong)UIButton * briTimeButton;

@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation BoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"称骨算命";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UILabel * topLabel = [UILabel new];
    
    topLabel.textColor = ColorWithRGB(0x8E6B44);
    topLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    topLabel.text = @"六，作六月算。称骨算命法，相传是唐朝周易大师袁天罡先生所创，其法将人的生辰八字，即出生的农历年月日时计算相应的“骨重”，然后根据“称骨”的总值来进行算命。上半月算本月，下半月算下月。如闰五月十四，作五月算；闰五月十六，作六月算。\n\n称骨算命法，相传是唐朝周易大师袁天罡先生所创即出生的农历年月日时计算相应的“骨重”，然后根据“称骨”的总值来进行算命。";
    topLabel.numberOfLines = 0;
    
    self.topLabel = topLabel;
    
    [self.view addSubview:self.topLabel];
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = ImageNamed(@"chenggu");
    
    self.rightImageView = rightImageView;
    
    [self.view addSubview:self.rightImageView];
    
    
    self.oneView = [self setupView];
    [self.view addSubview:self.oneView];
    self.twoView = [self setupView];
    [self.view addSubview:self.twoView];
    self.threeView = [self setupView];
    [self.view addSubview:self.threeView];
    self.fourView = [self setupView];
    [self.view addSubview:self.fourView];
    
    self.nameLabel = [self setupLabelWithText:@"姓名："];
    [self.oneView addSubview:self.nameLabel];
    self.genderLabel = [self setupLabelWithText:@"性别："];
    [self.twoView addSubview:self.genderLabel];
    self.briDateLabel = [self setupLabelWithText:@"出生年月："];
    [self.threeView addSubview:self.briDateLabel];
    self.briTimeLabel = [self setupLabelWithText:@"出生时辰："];
    [self.fourView addSubview:self.briTimeLabel];
    
    
    UITextField * nameTextFiled = [UITextField new];
    
    nameTextFiled.borderStyle = UITextBorderStyleNone;
    nameTextFiled.backgroundColor = WHITE_COLOR;
    //设置光标颜色
    nameTextFiled.tintColor = BLACK_COLOR;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    nameTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入您的姓名" attributes:attrs];
    nameTextFiled.textColor = BLACK_COLOR;
    nameTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    
    self.nameTextFiled = nameTextFiled;
    [self.view addSubview:self.nameTextFiled];
    
    self.genderButton = [self setupButtonWithTitle:@"请选择的您的性别"];
    [self.twoView addSubview:self.genderButton];
    self.briDateButton = [self setupButtonWithTitle:@"请选择的您的出生年月"];
    [self.threeView addSubview:self.briDateButton];
    self.briTimeButton = [self setupButtonWithTitle:@"请选择的您的出生时辰"];
    [self.fourView addSubview:self.briTimeButton];
    
    
    UIButton *sureButton = [UIButton new];
    
    [sureButton setBackgroundColor:ColorWithRGB(0x9D1E21)];
    [sureButton setTitle:@"立即测算" forState:UIControlStateNormal];
    [sureButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:21 * SCREENH6];
    sureButton.layer.cornerRadius = 5 * SCREENH6;
    sureButton.layer.masksToBounds = YES;
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(10 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-65 * SCREENH6);
        make.width.mas_equalTo(93 * SCREENH6);
        make.height.mas_equalTo(91 * SCREENH6);
        make.top.mas_equalTo(193 * SCREENH6);
    }];
    
    [self.oneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(40 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-40 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(300 * SCREENH6);
        make.height.mas_equalTo(39 * SCREENH6);
    }];
    
    [self.twoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneView);
        make.top.mas_equalTo(self.oneView.mas_bottom).offset(12.5 * SCREENH6);
    }];
    
    [self.threeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoView);
        make.top.mas_equalTo(self.twoView.mas_bottom).offset(12.5 * SCREENH6);
    }];
    
    [self.fourView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.threeView);
        make.top.mas_equalTo(self.threeView.mas_bottom).offset(12.5 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneView).offset(9 * SCREENH6);
        make.centerY.mas_equalTo(self.oneView);
    }];
    
    [self.nameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.oneView);
        make.trailing.mas_equalTo(self.oneView).offset(-10 * SCREENH6);
        make.leading.mas_equalTo(self.nameLabel.mas_trailing).offset(5 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.centerY.mas_equalTo(self.twoView);
    }];
    
    [self.genderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.genderLabel);
        make.leading.mas_equalTo(self.genderLabel.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.briDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.genderLabel);
        make.centerY.mas_equalTo(self.threeView);
    }];
    
    [self.briDateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.briDateLabel);
        make.leading.mas_equalTo(self.briDateLabel.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.briTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.briDateLabel);
        make.centerY.mas_equalTo(self.fourView);
    }];
    
    [self.briTimeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.briTimeLabel);
        make.leading.mas_equalTo(self.briTimeLabel.mas_trailing).offset(5 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.fourView);
        make.top.mas_equalTo(self.fourView.mas_bottom).offset(25 * SCREENH6);
        make.height.mas_equalTo(45 * SCREENH6);
    }];
}

- (UILabel *)setupLabelWithText:(NSString *)str{
    
    UILabel * label = [UILabel new];
    
    label.textColor = BLACK_COLOR;
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    label.text = str;
    
    return label;
}

- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0xB0B0B0) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickChooseButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    return button;
}


- (UIView *)setupView{
    
    UIView * view = [UIView new];
    
    view.backgroundColor = WHITE_COLOR;
    view.layer.cornerRadius = 5 * SCREENH6;
    view.layer.borderWidth = 1 * SCREENH6;
    view.layer.borderColor = [ColorWithRGB(0x675828) CGColor];
    view.userInteractionEnabled = YES;
    
    return view;
}


- (void)presentDateController {
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGPickerViewType2;
    datePicker.datePickerMode = PGDatePickerModeDateHourMinute;
    datePicker.language = @"zh-Hans";
    [self presentViewController:datePickManager animated:false completion:nil];
    
    //    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    //    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    //    datePicker.minimumDate = [dateFormatter dateFromString: @"2018-02-01 05:04:23"];
    //    datePicker.maximumDate = [dateFormatter dateFromString: @"2028-02-01 05:04:23"];
    
    //    NSDate *date = [dateFormatter dateFromString: @"2020-02-01 05:04:23"];
    //    [datePicker setDate:date animated: true];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    //    self.birthday = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    NSString * date = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)dateComponents.year,(long)dateComponents.month,dateComponents.day];
    NSString * time = [NSString stringWithFormat:@"%ld:%ld",(long)dateComponents.hour,(long)dateComponents.minute];
    
    [self.briDateButton setTitle:date forState:UIControlStateNormal];
    [self.briTimeButton setTitle:time forState:UIControlStateNormal];
    
    
}



- (void)clickSureButton{
    
    if (self.nameTextFiled.text.length >0 ) {
        
        HIDDEN_TAB;
        
        BaziLifeInfoViewController * infoVC = [BaziLifeInfoViewController new];
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请先完善所需信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}


- (void)clickChooseButton:(UIButton *)button{
    
    if (button == self.genderButton) {
        
        
        [self creatAlertController_sheet];
    }else{
        
        [self presentDateController];
    }
    
    
}


-(void)creatAlertController_sheet {
    
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"请选择性别" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //        self.gender = @"男";
        //
        //        [self.tableView reloadData];
        //
        //        [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:nil andBirthday:nil sex:@"男" resultWithBlock:^(BOOL success, NSDictionary * responseData) {
        //
        //
        //        }];
        [self.genderButton setTitle:@"男" forState:UIControlStateNormal];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //        self.gender = @"女";
        //
        //        [self.tableView reloadData];
        //
        //        [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:nil andBirthday:nil sex:@"女" resultWithBlock:^(BOOL success, NSDictionary * responseData) {
        //
        //        }];
        [self.genderButton setTitle:@"女" forState:UIControlStateNormal];
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    //把action添加到actionSheet里
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    [actionSheet addAction:action3];
    
    //相当于之前的[actionSheet show];
    [self presentViewController:actionSheet animated:YES completion:nil];
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
